//
//  ZTodoNotifyDetailVC.m
//  WeClubs
//
//  Created by Zed Link on 8/4/2017.
//  Copyright © 2017 Zed. All rights reserved.
//

#import "ZTodoNotifyDetailVC.h"
#import "ZTodoNotifyView.h"
#import "ZCommentCell.h"
#import "ZCommentModel.h"
#import "ZCommentHeader.h"


static NSString * const NotifyCommentCellReuseID = @"ZNotifyCommentCellReuseID";
static NSString * const CommentHeaderCellReuseID = @"ZCommentHeaderCellReuseID";

@interface ZTodoNotifyDetailVC () <UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet ZTodoNotifyView *notifyHeaderView;
@property (weak, nonatomic) IBOutlet UITableView *mainView;

/** 所有评论 */
@property (strong, nonatomic) NSArray *commentModels;

@end

@implementation ZTodoNotifyDetailVC

+ (instancetype)zNew
{
    return [self zStoryboardInstance];
}

- (void)zConfigNavigation
{
    [super zConfigNavigation];
    
    [self zAddBackItemWithGesture:YES];
    self.navigationItem.title = @"通知详情";
    
    
    
    self.navigationItem.rightBarButtonItem = [self zNavItemWithTitle:@"确认详情" andSelector:@selector(navRightOnClick)];
    [self zClearNavBgColorAndSeparator:NO];
    [self.navBgView setBackgroundColor:NAVBAR_BG_COLOR];
    [self.navSeparatorLine setImage:[UIImage imageNamed:@"shadow_nav"]];
}


- (void)zLoadSubview
{
    [super zLoadSubview];
    
    self.automaticallyAdjustsScrollViewInsets = NO;
    self.mainView.rowHeight = UITableViewAutomaticDimension;
    self.mainView.estimatedRowHeight = 60;
    self.mainView.sectionHeaderHeight = 36;
    self.mainView.separatorColor = SEPARATOR_BG_COLOR;
    
    [self.mainView registerClass:[ZCommentHeader class] forHeaderFooterViewReuseIdentifier:CommentHeaderCellReuseID];
}

- (void)viewDidLoad
{
    [super viewDidLoad];
    
    self.notifyHeaderView.model = self.notifyModel;
    #warning done
    NSDictionary *dataPara = @{
                               @"user_id" :@([ZUserInfo sharedInstance].userId),
                               @"token" : [ZUserInfo sharedInstance].token,
                               @"source_type" : @"notify",
                               @"source_id" : @(self.notifyModel.notifyId),
                               };
    [HTTPRequest POST:kGet_comment_listUrl parameter:dataPara success:^(id resposeObject) {
                NSArray *comments = resposeObject[@"data"][@"comment"];
        
                NSMutableArray *tempArrM = [NSMutableArray arrayWithCapacity:comments.count];
                for (NSDictionary *dict in comments) {
                    ZCommentModel *model = [[ZCommentModel alloc] initWithDictionary:dict];
                    [tempArrM addObject:model];
                }
                self.commentModels = tempArrM;
        
    } failure:^(NSError *error) {
       
    }];
    
    
//    [[ZNetManager sharedInstance] updateCommentsOfTodoItem:0 withItemId:self.notifyModel.notifyId success:^(ZNetManagerStatus status, NSString *resultMsg, NSDictionary *data)
//    {
//        // Todo: 加载下一页数据
//        NSArray *comments = data[@"comment"];
//        
//        NSMutableArray *tempArrM = [NSMutableArray arrayWithCapacity:comments.count];
//        for (NSDictionary *dict in comments) {
//            ZCommentModel *model = [[ZCommentModel alloc] initWithDictionary:dict];
//            [tempArrM addObject:model];
//        }
//        self.commentModels = tempArrM;
//        
//    } failure:nil];
}

#pragma mark - UITableViewDelegate & UITableViewDataSource

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    return 2;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return section == 1 ? self.commentModels.count : 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    ZCommentModel *commentModel = self.commentModels[indexPath.row];
    
    ZCommentCell *cell = [tableView dequeueReusableCellWithIdentifier:NotifyCommentCellReuseID];
    cell.commentModel = commentModel;

    return cell;
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    ZCommentHeader *header = [tableView dequeueReusableHeaderFooterViewWithIdentifier:CommentHeaderCellReuseID];
    header.style = ZTableHeaderStyleComment;
    header.alignment = ZTableHeaderAlignmentCenter;
    
    NSString *count = [NSString stringWithFormat:@"共%zd条回复", self.commentModels.count];
    NSDictionary *attr = @{
                           NSFontAttributeName : [UIFont systemFontOfSize:13 weight:UIFontWeightLight],
                           NSForegroundColorAttributeName : [UIColor zColorWithHex:0x666666],
                           };
    [header.descriptionBtn setAttributedTitle:[[NSAttributedString alloc] initWithString:count attributes:attr] forState:UIControlStateNormal];
    [header.descriptionBtn setImage:[UIImage imageNamed:@"ic_comment"] forState:UIControlStateNormal];
    
    return header;
}
- (void)navRightOnClick{
 
    
    
}

#pragma mark - Getter & Setter

- (void)setCommentModels:(NSArray *)commentModels
{
    _commentModels = commentModels;
    [self.mainView reloadData];
}

@end
